﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Moq;
using SportsStore.Domain.Entities;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Concrete;
    

namespace SportsStore.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel NinjectKernel { get; set; }

        public NinjectControllerFactory()
        {
            this.NinjectKernel = new StandardKernel();
            this.AddBindings();
        }

        protected override IController GetControllerInstance( RequestContext requestContext, Type controllerType )
        {
            return controllerType == null
                ? null
                : (IController) this.NinjectKernel.Get( controllerType );
        }

        private void AddBindings()
        {
            // TODO: Put Bindings Here
            this.NinjectKernel.Bind<IProductRepository>().To<EFProductRepository>();

            var emailSettings = new EmailSettings
            {
                WriteAsFile = bool.Parse( ConfigurationManager.AppSettings[ "Email.WriteAsFile" ] ?? "false" )
            };

            this.NinjectKernel.Bind<IOrderProcessor>()
                .To<EmailOrderProcessor>()
                .WithConstructorArgument( "settings", emailSettings );
        }
    }
}